package com.example.springboot004mybatis.mapper;

import com.example.springboot004mybatis.model.User;
import com.example.springboot004mybatis.provider.UserProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.jdbc.core.SqlProvider;

import java.util.List;

public interface UserMapper {
    /**
     * @Description: sql provider使用 和 xml文件提供的ResultMap
     * @auther: uu
     * @date: 2023/6/16 20:19
     */
    @ResultMap("userResultMap")
    @SelectProvider(type = UserProvider.class, method = "selectById")
    User selectByIdProvider(Integer id);

    /**
     * @Description: 根据id查询用户包括用户详情,一对一
     * @auther: uu
     * @date: 2023/6/16 1:44
     */

    @Select("""
            select * from t_user where id = #{id}
            """)
    @Results(id = "userOneToOne", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "id", property = "userInfo",
                    one = @One(select = "com.example.springboot004mybatis.mapper.UserInfoMapper.selectById",
                            fetchType = FetchType.LAZY)),
    })
    User selectById(Integer id);


    /**
     * @Description: 查询所有
     * @auther: uu
     * @date: 2023/6/16 18:08
     */
    @Select("select * from t_user")
    @ResultMap("userOneToOne")
    List<User> selectAll();


    /**
     * @Description: 增加用户
     * @auther: uu
     * @date: 2023/6/16 18:11
     */
    @Insert("""
            insert into t_user values(#{id},#{username},#{password},#{phone})
            """)
    @Results(id = "user", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(column = "phone", property = "phone"),
    })
    Integer insertUser(User user);
    
    /**
     * @Description: 根据用户id删除
     * @auther: uu
     * @date: 2023/6/16 18:14
     */

    @Delete("""
            delete from t_user where id = #{id}
            """)
    @ResultMap("user")
    Integer delById(Integer id);

    /**
     * @Description: 更新用户
     * @auther: uu
     * @date: 2023/6/16 18:20
     */
    @Update("""
            update t_user set username = #{username}, 
            password = #{password}, phone = #{phone}
            where id = #{id}
            """)
//    @ResultMap("user")
    @ResultMap("userResultMap")
    Integer updateUser(User user);
}
